package com.example.demo.array;

/**
 * 轮转数组
 */
public class RotateArray {

    public void rotate(int[] nums, int k) {
        int len = nums.length;
        k = k % len;

        int[] temp = new int[k];

        for (int i = 0; i < k; i++) {
            temp[i] = nums[len - k + i];
        }

        for (int i = len - k - 1; i >= 0; i--) {
            nums[i + k] = nums[i];
        }

        for (int i = 0; i < k; i++) {
            nums[i] = temp[i];
        }
    }

    public void rotate2(int[] nums, int k) {
        int len = nums.length;

        for (int i = 0; i < k; i++) {
            int tmp = nums[len - 1];

            System.arraycopy(nums, 0, nums, 1, len - 1);

            nums[0] = tmp;
        }
    }
}
